/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.persistentie;

import Server.domain.TheRandominator;
import Server.domain.items.Boots;
import Server.domain.items.ChestArmor;
import Server.domain.items.Equipment;
import Server.domain.items.EquipmentSetPiece;
import Server.domain.items.Gloves;
import Server.domain.items.Helmet;
import Server.domain.items.Item;
import Server.domain.items.ItemAttribute;
import Server.domain.items.ItemAttributeType;
import Server.domain.items.LegArmor;
import Server.domain.items.MeleeWeapon;
import Server.domain.items.Necklace;
import Server.domain.items.RangedWeapon;
import Server.domain.items.Ring;
import Server.domain.items.Weapon;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ripper
 */
public class ItemFactory 
{
    private int[] levels = {1,1,1000,3,6,3,5,15,15,15,15,
            25,5,15,35,15,75,30,10,4};
    
    private Item[] predefinedItems = {
            new Helmet("generic helmet", 10, 100000, 10, 10, new ArrayList<ItemAttribute>()),
            new MeleeWeapon("dagger", 10, 100001, 15, 2, 0, 45, 45, false, new ArrayList<ItemAttribute>()),
            new MeleeWeapon("zomgweapon", 1000000, 100002, 60, 60, 60, 50, 45, false, new ArrayList<ItemAttribute>()),
            new LegArmor("brown pants", 200, 100003, 24, 34, new ArrayList<ItemAttribute>(), EquipmentSetPiece.LEATHER),
            new LegArmor("frilly brown pants", 800, 100004, 33, 34, new ArrayList<ItemAttribute>(), EquipmentSetPiece.LEATHER),
            new Gloves("sharp fingernails", 50, 100005, 5, 0, new ArrayList<ItemAttribute>()),
            new Gloves("oven mits", 20, 100006, 2, 75, new ArrayList<ItemAttribute>()),
            new Ring("sapphire ring", 2500, 100007, 1, 1, new ArrayList<ItemAttribute>()),
            new Ring("ruby ring", 2500, 100008, 1, 1, new ArrayList<ItemAttribute>()),
            new Ring("emerald ring", 2500, 100009, 1, 1, new ArrayList<ItemAttribute>()),
            new Ring("amethyst ring", 2500, 100010, 1, 1, new ArrayList<ItemAttribute>()),
            new Boots("boots of poon", 1000, 1000012, 50, 50, new ArrayList<ItemAttribute>()),
            new Boots("red slippers", 50, 1000011, 5, 5, new ArrayList<ItemAttribute>()),
            new Boots("steel boots", 150, 1000013, 20, 20, new ArrayList<ItemAttribute>(), EquipmentSetPiece.STEEL),
            new ChestArmor("silver armor", 5000, 100018, 35, 10, new ArrayList<ItemAttribute>(), EquipmentSetPiece.SILVER),
            new ChestArmor("leather armor", 100, 100015, 20, 5, new ArrayList<ItemAttribute>(), EquipmentSetPiece.LEATHER),
            new ChestArmor("golden armor", 10000, 100014, 50, 20, new ArrayList<ItemAttribute>(), EquipmentSetPiece.GOLDEN),
            new Helmet("sapphire crown", 7500, 100016, 20, 5, new ArrayList<ItemAttribute>()),
            new Necklace("tooth chain necklace", 200, 100017, 2, 3, new ArrayList<ItemAttribute>()),
            new RangedWeapon("simple bow", 100, 100019, 50, 0, 0, 100, true, new ArrayList<ItemAttribute>())
            
            };
    
    public ItemFactory()
    {
        
    }
    
    public Item getRandomLootItemForLevel(int lvl)
    {
        Item i = null;
        
        int total = 0;
        int closestToLevelIndex = -1;
        int closestLvlDiff = Integer.MAX_VALUE;
        List<Integer> applicableness = new ArrayList<Integer>();
        for(int a = 0; a < levels.length;a++)
        {
            int lvlDiff = lvl - levels[a];
            if(lvlDiff < 0)
            {
                lvlDiff = -lvlDiff;
            }
            
            if(lvlDiff < closestLvlDiff)
            {
                closestToLevelIndex = a;
                closestLvlDiff = lvlDiff;
            }
            
            if(lvlDiff > 50)
            {
                lvlDiff = 50;
            }
            
            lvlDiff = 50 - lvlDiff;
            
            total += lvlDiff;
            applicableness.add(lvlDiff);
        }
        
        int index = -1;
        if(total == 0)
        {
            index = closestToLevelIndex;
        }
        else
        {
            int randomVal = TheRandominator.getRandom().nextInt(total);
            int counter = 0;
            index = -1;
            while(counter < randomVal)
            {
                index++;
                int val = applicableness.get(index);

                counter += val;
            }
        }

        Item predefItem = predefinedItems[index];

        Item newItem = predefItem.copyItem();

        //TODO: apply random factors to this new item!
        if(newItem instanceof Equipment)
        {
            //armor
            int current = ((Equipment)newItem).getArmorValue();
            int quarter = current / 4;
            if(quarter > 0)
            {
                current += (TheRandominator.getRandom().nextInt(quarter*2) - quarter);
            }

            if(current < 0)
            {
                current = 0;
            }
            ((Equipment)newItem).setArmorValue(current);

            //magic armor
            current = ((Equipment)newItem).getMagicArmorValue();
            quarter = current / 4;
            if(quarter > 0)
            {
                current += (TheRandominator.getRandom().nextInt(quarter*2) - quarter);
            }

            if(current < 0)
            {
                current = 0;
            }
            ((Equipment)newItem).setMagicArmorValue(current);

            //attack value (if it's a weapon)
            if(newItem instanceof Weapon)
            {
                current = ((Weapon)newItem).getPhysicalAttackValue();
                quarter = current / 4;

                current += (TheRandominator.getRandom().nextInt(quarter * 2) - quarter);
                if(current < 1)
                {
                    current = 1;
                }
                ((Weapon)newItem).setPhysicalAttackValue(current);
            }

            //random attribute
            boolean getsAnAttribute = true;
            while(getsAnAttribute)
            {
                getsAnAttribute = TheRandominator.getRandom().nextInt(5) == 2;
                if(getsAnAttribute)
                {
                    ItemAttribute ia = null;

                    //define ia
                    int randomIndex = 
                            TheRandominator.getRandom().nextInt(ItemAttributeType.getAllTypes().length);
                    ItemAttributeType type = 
                            ItemAttributeType.getAllTypes()[randomIndex];
                    int amount = lvl;
                    amount = (amount / 4) + 1;

                    amount = TheRandominator.getRandom().nextInt(amount) + 1;

                    double value = ItemAttributeType.getRecommendedMinimalImprovement()[randomIndex];
                    ia = new ItemAttribute(type, value * (double)amount);

                    ((Equipment)newItem).addEquipmentAttribute(ia);
                }
            }
        }
        i = newItem;
        
        return i;
    }
}
